const net = require('net');

// websocket  服务端原理

const server = net.createServer((socket) => {
  console.log('收到客户端的连接');
  // data 事件 发送消息头也会出发
  // 服务度 读一次 data 事件
  socket.once('data', (chunk) => {
    const httpContent = chunk.toString('utf-8');
    let parts = httpContent.split('\r\n');
    parts.shift();
    parts = parts
      .filter((s) => s)
      .map((s) => {
        const i = s.indexOf(':');
        return [s.substr(0, i), s.substr(i + 1).trim()];
      });
    const headers = Object.fromEntries(parts);
    const crypto = require('crypto');
    const hash = crypto.createHash('sha1');
    hash.update(headers['Sec-WebSocket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11');
    const key = hash.digest('base64');
    // 响应 特殊格式http 响应
    socket.write(`HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: ${key}

`);
    socket.on('data', (chunk) => {
      // 后面发送数据触发
      console.log(chunk);
    });
  });
});

server.listen(5008);
